package com.smmisia.article.factory;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smmisia.article.entity.Friend;
import com.smmisia.article.enums.FriendEnum;
import com.smmisia.article.exception.SocketException;
import com.smmisia.article.mapper.FriendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UnAcceptStrategy implements FriendStrategy{

    @Autowired
    private FriendMapper friendMapper;

    @Override
    public FriendEnum getFriendEnum() {
        return FriendEnum.FRIEND_STATUS_AGREE;
    }

    @Override
    public void updateFriendStatus(Integer id) {
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        LambdaUpdateWrapper<Friend> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(Friend::getUserId,loginIdAsInt)
                .eq(Friend::getFriendId,id)
                .set(Friend::getStatus,FriendEnum.FRIEND_STATUS_REFUSE.getCode());
        int update = friendMapper.update(null, wrapper);
        Assert.isTrue(update>0,()->new SocketException("更新好友状态失败"));
    }
}
